using System;
using System.Globalization;
using System.Windows.Data;

namespace SmartBoard.Core.Converters
{
    public static class EnumHelper
    {
        private static readonly StringToEnumValueConverter _converter = new StringToEnumValueConverter();

        public static string GetStringValue(string value, Type enumType)
        {
            return _converter.ConvertBack(value, enumType, null, null) as string;
        }

        public static object GetEnumValue(byte value, Type enumType)
        {
            return _converter.Convert(value.ToString(), enumType, null, null);
        }
    }

    public class StringToEnumValueConverter:IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var name = value as string;
            if (!string.IsNullOrEmpty(name))
            {
                return Enum.Parse(targetType, name, false);
            }
            return null; 
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var name = string.Empty;
            if (value != null)
            {
                name = Enum.GetName(value.GetType(), value);
            }
            return name;
        }
        #endregion
    }
}